using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using FileFolderNavigator;

namespace FolderBackupInterface
{
    public partial class Location : UserControl
    {
        private LocationElement loc;
        private bool _DisableAll;

        public bool DisableAll
        {
            get { return _DisableAll; }
            set
            {
                if (value)
                {
                    NameText.ReadOnly = true;
                    PathText.ReadOnly = true;
                }
                else
                {
                    NameText.ReadOnly = false;
                    PathText.ReadOnly = true;
                }
                _DisableAll = value;
            }
        }

        public string Path
        {
            get { return PathText.Text; }
            set { PathText.Text = value; }
        }

        public string Type
        {
            get { return TypeLabel.Text; }
            set { TypeLabel.Text = value; }
        }

        public string Name
        {
            get { return NameText.Text; }
            set { NameText.Text = value; }
        }

        public void SetValues(LocationElement elem)
        {
            loc = elem;
            Path = elem.Path;
            Type = elem.Type;
            Name = elem.Name;
        }

        public Location()
        {
            InitializeComponent();
            _DisableAll = false;
            PathText.ReadOnly = true;
            if(DisableAll)
            {
                PathText.ReadOnly = true;
                NameText.ReadOnly = true;
            }

            PathText.TextChanged += new EventHandler(PathText_TextChanged);
            NameText.TextChanged += new EventHandler(NameText_TextChanged);
        }

        void NameText_TextChanged(object sender, EventArgs e)
        {
            if (loc != null)
            {
                loc.Name = ((TextBox)sender).Text;
            }
        }

        void PathText_TextChanged(object sender, EventArgs e)
        {
            if (loc != null)
            {
                loc.Path = ((TextBox)sender).Text;
            }
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void Location_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.Type == "xml")
                {
                    DialogResult result = folderBrowserDialog1.ShowDialog();
                    if (result != DialogResult.Cancel)
                    {
                        this.Path = folderBrowserDialog1.SelectedPath + "\\";
                    }
                }
                else
                {
                    DialogResult result = folderBrowserDialog1.ShowDialog();
                    if (result != DialogResult.Cancel)
                    {
                        string n = folderBrowserDialog1.SelectedPath;
                        string p = n.Substring(0, n.LastIndexOf('\\') + 1);
                        string nn = n.Substring(n.LastIndexOf('\\') + 1);
                        this.Path = p;
                        this.Name = nn;
                    }
                }
            }
            catch (Exception)
            {
            }
        }
    }
}
